package com.powernode.strategy.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.constant.AuthConstants;
import com.powernode.domain.LoginSysUser;
import com.powernode.mapper.LoginSysUserMapper;
import com.powernode.model.SecurityUser;
import com.powernode.strategy.LoginStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * 后台管理系统登录策略的具体实现
 * @Author: DongGuoZhen
 * @Date: 2025/09/11/22:08
 * @Description:
 */
@Service(AuthConstants.SYS_USER_LOGIN)
public class SysUserLoginStrategy implements LoginStrategy {

    @Autowired
    private LoginSysUserMapper loginSysUserMapper;

    @Override
    public UserDetails realLogin(String username) {
//        根据用户名称查询用户对象
        LoginSysUser loginSysUser = loginSysUserMapper.selectOne(new LambdaQueryWrapper<LoginSysUser>()
                .eq(LoginSysUser::getUsername, username));
        if(ObjectUtil.isNotNull(loginSysUser)) {
//            根据用户标识查询用户的权限集合
            Set<String> perms = loginSysUserMapper.
                    selectPermsByUserId(loginSysUser.getUserId());

            SecurityUser securityUser = new SecurityUser();
            securityUser.setUserId(loginSysUser.getUserId());
            securityUser.setPassword(loginSysUser.getPassword());
            securityUser.setStatus(loginSysUser.getStatus());
            securityUser.setShopId(loginSysUser.getShopId());
            securityUser.setLoginType(AuthConstants.SYS_USER_LOGIN);
//            判断用户权限是否存在
            if(CollectionUtil.isNotEmpty(perms) && !perms.isEmpty()) {
                securityUser.setPerms(perms);
            }

            return securityUser;
        }
        return null;
    }
}
